package com.ruoyi.hr.service.impl.salaryadmin.salarydatechange;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.salaryadmin.SalaryAdminMapper;
import com.ruoyi.hr.mapper.salaryadmin.salarychange.SalaryChangeMapper;
import com.ruoyi.hr.mapper.salaryadmin.salarydatechange.SalaryDateChangeMapper;
import com.ruoyi.hr.service.salaryadmin.SalaryAdminService;
import com.ruoyi.hr.service.salaryadmin.salarydatechange.SalaryDateChangeService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SalaryDateChangeServiceImpl implements SalaryDateChangeService {

    @Autowired
    SalaryDateChangeMapper salaryDateChangeMapper;

    @Autowired
    SalaryAdminMapper salaryAdminMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findData(Map map, Authority authority) {
        List<String> proList = salaryAdminMapper.findProByPgid(map.get("pgid").toString());
        map.put("pro", proList);
        return salaryDateChangeMapper.findData(map, authority);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult updateProcedure(String allPeid, String EDdate) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allPeid.split(",");
            String msg;//异常信息
            for (String peid : array) {
                //根据PEID取出人名
                String name = salaryDateChangeMapper.findNameByPeid(peid);
                sql = "declare @RetVal varchar(50) exec CSP_ChangeEndDate " + peid + ",100," + maps.get("id").toString() + ",'" + EDdate + "',@RetVal output select @RetVal";
                String retVal = salaryDateChangeMapper.Procedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS,"修改日期成功！");
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }
}
